//
//  BPHomeTableViewCell.swift
//  BPWeiBo
//
//  Created by apple on 16/4/11.
//  Copyright © 2016年 apple. All rights reserved.
import UIKit
import SnapKit
class BPHomeTableViewCell: UITableViewCell {
    
    var tooBarTop:Constraint?
    
    //懒加载子视图控件
    lazy var homeUserView:BPHomeUserView = BPHomeUserView()
    lazy var bottomBarTool:BPBottomBarTool = BPBottomBarTool()
    lazy var trandmitView:BPTransmitView = BPTransmitView()
    
    //将每个cell对应的视图添加到我们的cell中,当我们cell中的homeuserView改变的时候,就设置成我们的每个cell对应的模型
    var cellViewModel: BPStatusCellViewModel? {
        //添加属性观察者
        didSet{
            homeUserView.statusCellViewModel = cellViewModel
            bottomBarTool.statusCellViewModel = cellViewModel
            //trandmitView.statusCellViewModel = cellViewModel
            
            //在更新约束之前先 卸载toolbar顶部的约束  先记录顶部约束
            self.tooBarTop?.uninstall()
            
            //MARK:判断是否有转发的微博,当有转发的微博的时候就讲转发微博显示,并且设置数据的来源
            if cellViewModel?.statusListModel?.retweeted_status != nil {
                //设置数据源
                trandmitView.statusCellViewModel = cellViewModel
                //设置显示
                trandmitView.hidden = false
                //修改约束
                bottomBarTool.snp_updateConstraints(closure: { (make) in
                   self.tooBarTop =  make.top.equalTo(trandmitView.snp_bottom).constraint
                })
            }else {
                //设置数据源为空
                trandmitView.statusCellViewModel = nil
                //设置隐藏
                trandmitView.hidden  = true
                //更新约束
                bottomBarTool.snp_updateConstraints(closure: { (make) in
                    self.tooBarTop = make.top.equalTo(homeUserView.snp_bottom).constraint
                })
            }
            

        }
    }

    //构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Default, reuseIdentifier: reuseIdentifier)
        setUpUI()
        
        //设置选中的颜色
        selectionStyle = .None
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //MARK:设置界面
    private func setUpUI() {
        
        //设置背景色
        backgroundColor = UIColor(white: 0.9, alpha:1)
        //添加子视图
        self.contentView.addSubview(homeUserView)
        self.contentView.addSubview(bottomBarTool)
        self.contentView.addSubview(trandmitView)
            //设置子视图的约束
            homeUserView.snp_makeConstraints { (make) in
                make.top.left.right.equalTo(contentView)
                //make.height.equalTo(100)
            }
            bottomBarTool.snp_makeConstraints { (make) -> Void in
                //设置约束,并且记录约束的对象
               self.tooBarTop = make.top.equalTo(trandmitView.snp_bottom).constraint
                make.height.equalTo(36)
                make.left.right.equalTo(contentView)
            }
            trandmitView.snp_makeConstraints { (make) in
                make.left.right.equalTo(contentView)
                make.top.equalTo(homeUserView.snp_bottom)
                //make.height.equalTo(80)
            }

        //添加contentView的底部约束
         //snp_makeConstraints  - make 屏蔽了frame布局
        contentView.snp_makeConstraints { (make) in
            //其他的几个约束也需要同时添加 不然contentView只知道底部在哪个地方
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(bottomBarTool.snp_bottom).offset(StatusCellMargin)
        }
    }
}
